/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.result;


import java.util.*;

public class DatabaseResult extends Result implements Comparable<DatabaseResult> {
	public DatabaseResultEntry[] entries = new DatabaseResultEntry[0];
	public String replyTo = "";

	public DatabaseResult(Vector<DatabaseResultEntry> entries) {
		this.entries = new DatabaseResultEntry[entries.size()];
		for (int i = 0; i < this.entries.length; i++) {
			this.entries[i] = entries.elementAt(i);
		}
	}
	
	public DatabaseResult() {
	
	}
	
	public DatabaseResultEntry getEntry(int type) {
		for (int i = 0 ; i < entries.length; i++)
			if (entries[i].type == type)
				return entries[i];
		return null;	
	}
	
	public void addEntry(DatabaseResultEntry dbre) {
		DatabaseResultEntry[] temp = new DatabaseResultEntry[entries.length + 1];
		for (int i = 0; i < entries.length; i++) {
			if (entries[i].compareTo(dbre) == 0)
				return;
			temp[i] = entries[i];
		}
		temp[temp.length-1]  = dbre;
		entries = temp;
	}

	public int compareTo(DatabaseResult dbr) {
		try {
			if (dbr.entries.length != entries.length)
				return -1;
			for (int i = 0; i < entries.length; i++) {
				boolean found = false;
				for (int j = 0; j < entries.length; j++) {
					if (entries[j].compareTo(dbr.entries[j]) == 0) {
						found = true;
						break;
					}
				}
				if (!found) {
					return -1;
				}
			}
		} catch (ClassCastException cce) {
		}
		return 0;
	}

	public String toXML() {
		String temp = "<DatabaseResult";
		if (replyTo.length() > 0)
			temp += " replyTo=\"" + replyTo + "\"";
		temp += ">";
		for (int i = 0; i < entries.length; i++)
			temp += entries[i].toXML();
		temp += "</DatabaseResult>";
		return temp;
	}


}
